// 引入express
const express = require("express");
// 引入数据库
const sql = require("../pool.js");
// 创建路由器对象
const route = express.Router();
// 研学互动内容增加路由
route.post("/interactAdd", (req, res) => {
  sql.query("insert into zm_interact set ?", [req.body], (err,r) => {
    if (err) {
      throw err;
    }
    console.log(r);
	console.log(req.body)
  
  if (r.affectedRows) {
    res.send("增加成功!");
  } else {
    res.send("增加失败!");
  }
});
});
// 游玩内容增加
route.post("/playContent",(req,res)=>{
  sql.query("insert into zm_play set ?",[req.body],(err,r)=>{
    if(err){
      throw err
    }
    if(r.affectedRows){
      res.send("增加成功!")
    }else{
      res.send("增加失败!")
    }
  })
})
// 游玩内容更改
route.put("/playUpdate/:pid",(req,res)=>{
  let pid = parseInt(req.params.pid)
  sql.query("update zm_play set ? where zm_play.pid = ?",[req.body,pid],(err,r)=>{
    if(err){
      throw err
    }
    if(r.affectedRows){
      res.send("更改成功!")
    }else{
      res.send("更改失败!")
    }
  })
})
// 研学互动内容查询
route.get("/interactQuery",(req,res)=>{
  // let pid = parseInt(req.params.pid)
  sql.query("select * from zm_interact",(err,r)=>{
    if(err){
      throw err
    }
    if(r.length){
      res.send({code:200,msg:'查询成功',data:r})
    }else{
      res.send({code:400,msg:'查询失败'})
    }
  })
})
// 游玩内容删除路由
route.get("/playDelete/:pid", (req, res) => {
  let pid = parseInt(req.params.pid)
  sql.query(
    "delete from zm_play where zm_play.pid = ?",
    [pid],
    (err, r) => {
      if (err) {
        throw err;
      }
      console.log(r);

      if (r.affectedRows) {
        res.send("删除成功!");
      } else {
        res.send("删除成功!");
      }
    }
  );
});
module.exports = route